﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace LenderClearingHouse
{
  public partial class Login : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      lbError.Visible = false;
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
      SetFocus(tbLogin);
    }

    protected void loginBox_Authenticate(object sender, ImageClickEventArgs e)
    {
      try
      {
        User u = Authentication.ValidateUser(tbLogin.Text, tbPassword.Text);
        Session["User"] = u;

        FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, u.UserId, DateTime.Now, DateTime.Now.AddMinutes(1439200), true, u.UserId, FormsAuthentication.FormsCookiePath);
        string hash = FormsAuthentication.Encrypt(Authticket);
        HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
        Response.Cookies.Add(Authcookie);

        //if (u.IsAdmin)
        if (u.UserRole == UserRole.SuperAdmin)
        {
          Response.Redirect("~/MasterList.aspx");
        }
        else if (u.UserRole == UserRole.ProjectManager)
        {
          Response.Redirect("~/ProjectManagerList.aspx");
        }
        else if (u.UserRole == UserRole.InvestorAdmin)
        {
            if (u.SubRole == UserSubRole.LVReo) Response.Redirect("~/LvreoMasterList.aspx");
            else Response.Redirect("~/MasterList.aspx"); //Response.Redirect("~/SdreoMasterList.aspx");
        }
        else if (u.UserRole == UserRole.InvestorUser)
        {
          Response.Redirect("~/MyInvestments.aspx");
        }
        else if (u.UserRole == UserRole.EscrowUser)
        {
            Response.Redirect("~/EscrowList.aspx");
        }
        /////////////////
        else if (u.LvreoRole != RoleAccess.NoAccess)
        {
          Response.Redirect("~/LvreoMasterList.aspx");
        }
        else if (u.SdreoRole != RoleAccess.NoAccess)
        {
          Response.Redirect("~/SdreoMasterList.aspx");
        }
        else if (u.MasterlistRole != RoleAccess.NoAccess)
        {
          Response.Redirect("~/MasterList.aspx");
        }
        else throw new Exception("No rights");
      }
      catch (Exception ex)
      {
        lbError.Visible = true;
        lbError.Text = ex.Message;
      }

      /*if (u.Role.Equals("admin"))
      {
        FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, u.UserId, DateTime.Now, DateTime.Now.AddMinutes(1439200), true, u.Role, FormsAuthentication.FormsCookiePath);
        string hash = FormsAuthentication.Encrypt(Authticket);
        HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
        Response.Cookies.Add(Authcookie);
        Response.Redirect("~/AdminMasterList.aspx");

      }
      else if (u.Role.Equals("user"))
      {
        FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, u.UserId, DateTime.Now, DateTime.Now.AddMinutes(1439200), true, u.Role, FormsAuthentication.FormsCookiePath);
        string hash = FormsAuthentication.Encrypt(Authticket);
        HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

        Response.Cookies.Add(Authcookie);
        Response.Redirect("~/UsersMasterList.aspx");
      }
      else if (u.Role.Equals("sdreo"))
      {
        FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, u.UserId, DateTime.Now, DateTime.Now.AddMinutes(1439200), true, u.Role, FormsAuthentication.FormsCookiePath);
        string hash = FormsAuthentication.Encrypt(Authticket);
        HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

        Response.Cookies.Add(Authcookie);
        Response.Redirect("~/SdreoList.aspx");
      }
      else if (u.Role.Equals("lvreo"))
      {
        FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, u.UserId, DateTime.Now, DateTime.Now.AddMinutes(1439200), true, u.Role, FormsAuthentication.FormsCookiePath);
        string hash = FormsAuthentication.Encrypt(Authticket);
        HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

        Response.Cookies.Add(Authcookie);
        //Response.Redirect("~/LvreoList.aspx");
        Response.Redirect("~/LvreoList.aspx");
      }
      else
      {
        lbError.Visible = true;
        lbError.Text = "Invalid username or password";
        //lblMessage.Text = "Invalid username or password";
      }*/
    }
  }
}